import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/exam/widgets/BGExamWidget.dart';
import '../../controller/assessments.dart';
import '../../exam/dto/assessment.response.dart';
import '../../widgets/buttons/debouncedMaterialButton.dart';

class SwitchGraded extends StatefulWidget {
  SwitchGraded(
      {Key? key,
      required this.questionItem,
      this.partType,
      required this.questionId,
      required this.nanhaiBookKnowledgeTypes,
      this.getAnswer,
      required this.mode,
      required this.isTeacherOrSchoolAdmin,
      required this.recordStatus,
      this.saveAnswers})
      : super(key: key);
  final QuestionItem questionItem;
  final String? partType;
  final String questionId;
  final NanhaiBookKnowledgeTypes nanhaiBookKnowledgeTypes;
  final BGExamWidgetMode mode;
  final Function(String)? getAnswer;
  final bool isTeacherOrSchoolAdmin;
  final String recordStatus;
  final Function()? saveAnswers;
  @override
  State<SwitchGraded> createState() => _SwitchGraded();
}

class _SwitchGraded extends State<SwitchGraded> {
  final AssessmentsController _assessmentsController = Get.find();
  bool switchSave = false;
  String writtenString = '';
  String oralString = '';
  String speechString = '';
  String translateString = '';
  List<Map> writtenJson = [];
  List<Map> speechJson = [];
  List<Map> oralJson = [];
  List<Map> translateJson = [];
  @override
  void initState() {
    // set the grade and comment if it is graded
    if (widget.recordStatus == 'COMPLETED' &&
        widget.nanhaiBookKnowledgeTypes ==
            NanhaiBookKnowledgeTypes.quickReview) {
      if (widget.partType == 'SPEAKING') {
        oralString = _assessmentsController
                .assessmentRecordsGrades.value.examLibRecord?.oralJson ??
            '';
        if (oralString != '') {
          oralJson = json.decode(oralString).cast<Map<dynamic, dynamic>>();
          oralJson.forEach((element) {
            if (element['questionId'] == widget.questionId) {
              widget.questionItem.score = element['score'];
              widget.questionItem.comment = element['comment'];
            }
          });
        }
      } else if (widget.partType == 'READING') {
        translateString = _assessmentsController
                .assessmentRecordsGrades.value.examLibRecord?.translateJson ??
            '';
        if (translateString != '') {
          translateJson =
              json.decode(translateString).cast<Map<dynamic, dynamic>>();
          translateJson.forEach((element) {
            if (element['questionId'] == widget.questionId) {
              widget.questionItem.score = element['score'];
              widget.questionItem.comment = element['comment'];
            }
          });
        }
      }
    }
    super.initState();
  }

  @override
  void dispose() {
    Get.delete<SwitchGraded>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        BGExamWidget(
            questionItem: widget.questionItem,
            getAnswer: widget.getAnswer,
            questionId: widget.questionId,
            mode: widget.mode,
            switchSave: switchSave),
        SizedBox(height: 15),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Visibility(
                visible: widget.isTeacherOrSchoolAdmin == false &&
                    widget.recordStatus != 'COMPLETED',
                child: DebouncedMaterialButton(
                    debounceTimeMs: 800,
                    color: Theme.of(context).colorScheme.secondary,
                    onPressed: () {
                      widget.saveAnswers!();
                    },
                    child: Text('Save'.tr,
                        style: TextStyle(color: Colors.white)))),
          ],
        )
      ],
    );
  }
}
